﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

public partial class import : System.Web.UI.Page
{
    protected FileCollection files;
    protected FileUpload[] uploads;
    protected Label[] uploaderrors;

    /// <summary>
    /// initializes the page
    /// </summary>
    public import()
    {
        files = new FileCollection();
    }

    /// <summary>
    /// Creates the array of FileUpload controls used to actually send the files
    /// </summary>
    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        // create the array, and add them to the (invisible, so not rendered) placeholder
        // without adding them to the placeholder, they are not linked up to postback events
        // and they don't work!
        uploads = new FileUpload[files.Count];
        for (int i = 0; i < uploads.Length; i++)
        {
            uploads[i] = new FileUpload();
            uploads[i].ID = "file" + i;
            uploads[i].EnableViewState = true;
            placeholder.Controls.Add(uploads[i]);
        }

        uploaderrors = new Label[files.Count];
        for (int i = 0; i < uploaderrors.Length; i++)
        {
            uploaderrors[i] = new Label();
            uploaderrors[i].ID = "fileerror" + i;
            uploaderrors[i].EnableViewState = true;
            uploaderrors[i].ForeColor = System.Drawing.Color.Red;
            placeholder.Controls.Add(uploaderrors[i]);
        }
    }

    /// <summary>
    /// Called when the page is rendered
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs ea)
    {
        message.Text = successmsg.Text = String.Empty;

        // we only care if the page is being posted back to us
        if (!IsPostBack)
            return;

        // upload password, for an extra (minimal) level of safety
        if (password.Text != "williams#law")
        {
            message.Text = "Invalid upload password";
            return;
        }

        string[] fileErrors = new string[files.Count];
        bool anyErrors = false;

        // check for file uploads
        for (int i = 0; i < uploads.Length; i++)
        {
            // was a file uploaded?
            if (!uploads[i].HasFile)
                continue;

            // get the data
            HttpPostedFile file = uploads[i].PostedFile;
            StreamReader sr = new StreamReader(file.InputStream);

            // and process it
            uploaderrors[i].Text = String.Empty;
            try
            {
                if (!files[i].ProcessData(sr))
                {
                    uploaderrors[i].Text = "Unspecified error processing file";
                    anyErrors = true;
                }
            }
            catch (InvalidDataException e)
            {
                uploaderrors[i].Text = "Data invalid: " + e.Message;
                anyErrors = true;
            }
            catch (Exception e)
            {
                uploaderrors[i].Text = "Miscellaneous error: " + e.GetType().Name + " - " + e.Message;
                anyErrors = true;
            }
        }

        if (anyErrors)
            message.Text = "Errors occurred while processing the data";
        else
            successmsg.Text = "Data uploaded successfully";
    }
}